<?php

/*
 * Action - Reaction 2$
 *
 * Gain 3 coppers, putting them in your hand
 * When another player plays an Attack card, you may discard this. If you do, gain two Silvers, putting one on top of your deck
 */

class Beggar extends Card implements iAction, iKingdom, iDarkAges, iReaction, iReactingCard {

    public function __construct() {
        parent::__construct("Beggar", 2);
        $this->undoPlay = false;
        $this->undoBuy = true;

        $this->reactConditions = new ReactionConditions();
        $this->reactConditions->action = array(Attack);
        $this->reactConditions->actor = array(SomeoneElse);
        $this->reactConditions->cardLocation = array(PlayHand);
        $this->reactConditions->limit = OncePerReaction;
    }

    public function getConditions() {
        return $this->reactConditions;
    }

    public function isReactRelevant(Game $game, $card, $name) {
        return true;
    }

    public function react(Game $game, $card, $name) {
        ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($this), "reveals " . nicePrint($this), $name);

        $options = array(new Option(1, "Discard Beggar"), new Option(2, "Leave Beggar in your hand"));
        $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Please choose", $name);
        $answer = $output[0];
        if ($answer == "Discard Beggar") {
            $game->moveCardFromPileToPile($this, PlayHand, DiscardPile, $name);
            $s1 = $game->moveCardFromPileToPile("Silver", BoardCards, DrawPile, $name);
            $s2 = $game->moveCardFromPileToPile("Silver", BoardCards, DiscardPile, $name);
            $silvers = array();
            if ($s1)
                $silvers[] = $s1;
            if ($s2)
                $silvers[] = $s2;

            if (sizeof($silvers) == 2) {
                ScreenDisplay::addScreenMessage($game, true, "discard ".  nicePrint($this)." and gain ".  nicePrint($silvers).", putting one of them on top of your deck", 
                         "discards ".  nicePrint($this)." and gains ".  nicePrint($silvers).", putting one of them on top of his deck", $name);
            } elseif ($s1 && $s2) {
                ScreenDisplay::addScreenMessage($game, true, "discard ".  nicePrint($this)." and gain ".  nicePrint($silvers).", putting it on top of your deck", 
                         "discards ".  nicePrint($this)." and gains ".  nicePrint($silvers).", putting it on top of his deck", $name);
            } else {
                ScreenDisplay::addScreenMessage($game, true, "discard  ".  nicePrint($this)." and gain nothing", "discards ".  nicePrint($this)." and gains nothing", $name);
            }
        }
        $game->setTempCard($card);
        return true;
    }

    public function getDescription() {
        return array(
            "Gain 3 coppers, putting them in your hand",
            "When another player plays an Attack card, you may discard this. If you do, gain two Silvers, putting one on top of your deck");
    }

    public function play(Game $game, $isRealCard) {
        $s1 = $game->moveCardFromPileToPile("Copper", BoardCards, PlayHand);
        $s2 = $game->moveCardFromPileToPile("Copper", BoardCards, PlayHand);
        $s3 = $game->moveCardFromPileToPile("Copper", BoardCards, PlayHand);

        $coppers = array();
        if ($s1)
            $coppers[] = $s1;
        if ($s2)
            $coppers[] = $s2;
        if ($s3)
            $coppers[] = $s3;

        ScreenDisplay::addScreenMessage($game, true, "gain " . nicePrint($coppers). "putting them in your hand", " gains" . ScreenDisplay::$LMSG." putting them in his hand");
        $game->cardActionFinished();
        return true;
    }

}

?>